function [idat sandbox scaninfo] = fsb_preload_sandbox(loadstruct)

% FSB : Load data into sandbox environment when starting up
%
% EXAMPLE:
% [idat sandbox scaninfo] = fsb_preload_sandbox(loadstruct)
%
% INPUT:
% loadstruct
% loadstruct.idat :     4-D image dataset or string
% loadstruct.scannum:   Number of scan to load
% loadstruct.reconum:   Reconstruction number of scan to load
% loadstruct.fidopt:    If the reconstruction of the scan should be done in
%                       fMRI Sandbox
%
% OUTPUT:
% idat:         4-D image data
% sandbox:      sandbox experiment struct
%
% CALLED BY:
% FSB.m
%
% NOTES:
% Essential component of fMRI Sandbox
%
% Copyright 2010 MPI for Biological Cybernetics
% Author: Steffen Stoewer
% License:GNU GPL, no express or implied warranties
%
% $Revision 1.0
%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

%~~~~~~~~~~~~~~~~~~~~
% is idat field present?
%~~~~~~~~~~~~~~~~~~~~
try
    if isfield(loadstruct,'idat')==1;
        idat = loadstruct.idat;

        if max(idat(:))>intmax('int16')& min(idat(:))>0;
            idat = idat/max(idat(:))*32767;
            idat = int16(idat);
        end

        if ~ischar(idat) & min(idat(:)>=0 & max(idat(:)>100));
            idat = int16(idat);
        end

        %~~~~~~~~~~~~~~~~~~~~
        % is idat a char array?
        %~~~~~~~~~~~~~~~~~~~~
        if ischar(idat)

            [pathstr, name, ext, versn] = fileparts(idat);

            %~~~~~~~~~~~~~~~~~~~~
            % Look for extension
            %~~~~~~~~~~~~~~~~~~~~
            switch(ext)

                case {'.hdr', '.img'}
                    %~~~~~~~~~~~~~~~~~~~~
                    % Load analyze files
                    %~~~~~~~~~~~~~~~~~~~~
                    nidat = avw_img_read(idat);
                    sandbox.hinfo = analyze75info('name');
                    idat = nidat.img;


                case '.mat'
                    %~~~~~~~~~~~~~~~~~~~~
                    % Load mat files
                    %~~~~~~~~~~~~~~~~~~~~
                    tempdat = load(idat);
                    if isfield(tempdat,'idat')
                        idat = tempdat.idat;
                    else
                        idat = int16(zeros(96,96,20,100));
                    end

                    if isfield(tempdat,'sandbox');
                        %~~~~~~~~~~~~~~~~~~~~
                        % Load sandbox
                        %~~~~~~~~~~~~~~~~~~~~
                        sandbox = tempdat.sandbox;
                    end
            end

        elseif isnumeric(idat)==1;
            %~~~~~~~~~~~~~~~~~~~~
            % idat array exists
            %~~~~~~~~~~~~~~~~~~~~

            sandbox.hdr.dime.pixdim(2)=99;
            sandbox.hdr.dime.pixdim(3)=99;
            sandbox.hdr.dime.pixdim(4)=99;
            scaninfo.hdr = [];
        end


    else
        %~~~~~~~~~~~~~~~~~~~~
        % no idat array exists
        %~~~~~~~~~~~~~~~~~~~~
        idat = int16(zeros(96,96,16,150));
        sandbox.hdr.dime.pixdim(2)=99;
        sandbox.hdr.dime.pixdim(3)=99;
        sandbox.hdr.dime.pixdim(4)=99;
        scaninfo.hdr = [];
    end

    %~~~~~~~~~~~~~~~~~~~~
    % This is for MPI internal use only
    %~~~~~~~~~~~~~~~~~~~~

    if isfield(loadstruct,'scannum')==1;
        scannum = loadstruct.scannum;

        if ~isfield(loadstruct,'reconum')==1;
            reconum =1;
        else
            reconum = loadstruct.reconum;
        end

        [scanpar STDPATH] = fsb_get_scaninfo(idat,scannum,reconum);
        
        % Get additional information from fsb_scan_protocol
        scandata = fsb_scan_protocol(idat,scannum,scannum);

        if isempty(STDPATH.pv);
            disp('STDPATH variable not set correctly, aborting')
            idat = zeros(10,10,10);
            return
        end

        if isfield(loadstruct,'fidopt')==0 || loadstruct.fidopt == 0;
            idat = PVrd2dseq(idat,scannum,opt( 'RECO',reconum,'VERBOSE',0));
        end

        if isfield(loadstruct,'fidopt')==1;
            if isfield(loadstruct,'recox')==1;
                recox = loadstruct.recox;
            else
                recox = 0;
            end
            if isfield(loadstruct,'recoy')==1;
                recoy = loadstruct.recoy;
            else
                recoy = 0;
            end
            switch loadstruct.fidopt
                case 1
                    data = reco_kusmia4(idat,scannum,24,0,0,recox,recoy,1);
                    idat = data.absdata;
                case 2
                    data = reco_kusmia4(idat,scannum,20,0,recox,recoy,1,0);
                    idat = data.absimages;
            end
        end

        dim = size(idat);

        %~~~~~~~~~~~~~~~~~~~~
        % Sandbox parameters
        %~~~~~~~~~~~~~~~~~~~~

        sandbox.discard = [];
        sandbox.scanpar.scanpath = STDPATH.fsb;
        sandbox.scandata = scandata;

        sandbox.scanpar = scanpar;
        sandbox.hdr = scanpar.hdr;
        if strcmp(scandata.method,'FLASH')==1
            sandbox.scanpar.TR = scanpar.TR;
            sandbox.TR = scanpar.TR/1000;
        else
            sandbox.scanpar.TR = scanpar.TR*scanpar.acqp.PVM_EpiNShots;
            sandbox.TR = sandbox.scanpar.TR/1000;
        end

        %~~~~~~~~~~~~~~~~~~~~
        % Try to load .rtp files from the scanner, if present
        %~~~~~~~~~~~~~~~~~~~~
        try
            filename = [STDPATH.fsb '/am.rtp'];
            sandbox.art_mean_x = zeros(size(idat,4),1);
            out = fsb_rtp_read(sandbox, filename);
            sandbox = out.sandbox;
        catch
            % disp('No rtp file present');
        end

        %~~~~~~~~~~~~~~~~~~~~
        % Scaninfo parameters
        %~~~~~~~~~~~~~~~~~~~~
        scaninfo.hdr = scanpar.hdr;
        scaninfo.scanpar = scanpar;
        scaninfo.scanpar.TR = scanpar.TR*scanpar.acqp.PVM_EpiNShots;
        scaninfo.TR = scanpar.TR;
        scaninfo.NSegments = scanpar.acqp.PVM_EpiNShots;
        scaninfo.InterVolumeTime = scanpar.TR*scanpar.acqp.PVM_EpiNShots;
        scaninfo.offset = min(scanpar.acqp.ACQ_slice_offset)/scanpar.hdr.dime.pixdim(4);
        scaninfo.origin = [dim(1)/2 dim(2)/2 scaninfo.offset 0 0];
    end
catch
    disp('File could not successfully be read, aborting')
end

end
